/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 * @author zezz
 */
public class HighScore extends Canvas implements CommandListener {

    /**
     * constructor
     */
    public int[] scores = new int[10];
    private MidletDuaXe midlet;
    private Display display;
    private Image bg, imgTitle, img1, img2, img3, img4, img5, img6, img7, img8, img9, img10;
    public Command cmdExit = new Command("Exit", Command.EXIT, 0);
    public Command cmdBack = new Command("Back", Command.BACK, 0);

    public HighScore(MidletDuaXe midlet, Display display) {
        try {

            this.midlet = midlet;
            this.display = display;
            loadData();

            // Set up this canvas to listen to command events
            setCommandListener(this);
            // Add the Exit command
            addCommand(cmdExit);
            addCommand(cmdBack);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public HighScore() {
        loadData();
    }

    /**
     * paint
     */
    public void paint(Graphics g) {
        try {
            bg = Image.createImage("/Images/bg_Highscore.png");
            imgTitle = Image.createImage("/Images/title_highscore.png");
            img1 = Image.createImage("/Images/hs1.png");
            img2 = Image.createImage("/Images/hs2.png");
            img3 = Image.createImage("/Images/hs3.png");
            img4 = Image.createImage("/Images/hs4.png");
            img5 = Image.createImage("/Images/hs5.png");
            img6 = Image.createImage("/Images/hs6.png");
            img7 = Image.createImage("/Images/hs7.png");
            img8 = Image.createImage("/Images/hs8.png");
            img9 = Image.createImage("/Images/hs9.png");
            img10 = Image.createImage("/Images/hs10.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        g.drawImage(bg, 0, 0, g.LEFT | g.TOP);
        g.drawImage(imgTitle, 0, 5, g.LEFT | g.TOP);
        g.drawImage(img1, 20, 31, g.LEFT | g.TOP);
        g.drawImage(img2, 20, 57, g.LEFT | g.TOP);
        g.drawImage(img3, 20, 83, g.LEFT | g.TOP);
        g.drawImage(img4, 20, 109, g.LEFT | g.TOP);
        g.drawImage(img5, 20, 135, g.LEFT | g.TOP);
        g.drawImage(img6, 20, 161, g.LEFT | g.TOP);
        g.drawImage(img7, 20, 187, g.LEFT | g.TOP);
        g.drawImage(img8, 20, 213, g.LEFT | g.TOP);
        g.drawImage(img9, 20, 239, g.LEFT | g.TOP);
        g.drawImage(img10, 20, 265, g.LEFT | g.TOP);
        int j = 35;
        for (int i = 0; i < scores.length; i++) {
            g.setColor(255, 255, 255);
            g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE));
            g.drawString(Integer.toString(scores[i]), 60, j, g.LEFT | g.TOP);
            j += 26;
        }
      //   g.drawString("Back", 10, 300, g.LEFT | g.TOP);



    }

    protected void keyPressed(int keyCode) {
    }

    protected void keyReleased(int keyCode) {
    }

    protected void keyRepeated(int keyCode) {
    }

    protected void pointerDragged(int x, int y) {
    }

    protected void pointerPressed(int x, int y) {
    }

    protected void pointerReleased(int x, int y) {
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == cmdBack) {
            display.setCurrent(new MenuGame(midlet, display));
        }
        if(command==cmdExit)
        {
             midlet.destroyApp(true);
        }
    }
//kiem tra diem de cho vao top

    public int isHighScore(int score) {
        for (int i = 0; i < scores.length; i++) {
            if (score > scores[i]) {
                return i;
            }
        }
        return -1;
    }
//khoi tao 

    public void initOptions() {
        // if (scores == null) {
        //  scores = new Score[10];
        for (int i = 0; i < scores.length; i++) {
            scores[i] = 0;
        }
        // }
        //loadData();
    }

    public void saveData() {

        try {
            RecordStore options = RecordStore.openRecordStore("higtScore", true);
            byte[] data = saveOptions();
            if (options.getNumRecords() != 0) // record store not empty
            {
                options.setRecord(1, data, 0, data.length);
            } else {
                options.addRecord(data, 0, data.length);
            }
            options.closeRecordStore();
        } catch (RecordStoreException ex) {
        }
    }

    public byte[] saveOptions() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        scores = Sort(scores);
        try {
            // write scores
            for (int i = 0; i < 10; i++) {
                dos.writeInt(scores[i]);
                // dos.writeUTF(scores[i].name);
            }
            dos.flush();
        } catch (IOException ex) {
        }
        return baos.toByteArray();
    }

    public int[] Sort(int[] highScore) {
        for (int i = 0; i < highScore.length; i++) {
            for (int j = i + 1; j < highScore.length; j++) {
                if (highScore[i] < highScore[j]) {
                    int temp = highScore[i];
                    highScore[i] = highScore[j];
                    highScore[j] = temp;
                }
            }
        }
        return highScore;
    }

    public void saveScore(int score) {
        if (isHighScore(score) > -1) {
            scores[9] = score;
            saveData();
        }
    }

    public void loadData() {
        try {
            RecordStore options = RecordStore.openRecordStore("higtScore", true);
            // check if record store not empty
            if (options.getNumRecords() != 0) {
                loadOptions(options.getRecord(1));
            }
            else
                initOptions();
            options.closeRecordStore();
        } catch (RecordStoreException ex) {
        }
    }

    public void loadOptions(byte[] data) {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DataInputStream dis = new DataInputStream(bais);
        try {
            for (int i = 0; i < scores.length; i++) {
                int value = dis.readInt();
                // String name = dis.readUTF();
                // scores[i] = new Score(value, name);
                scores[i] = value;
            }
            dis.close();
        } catch (IOException ex) {
        }
    }
}
